<template>
  <div id="app">
    {{ answer }}
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      answer: {},
      timer: "",
    };
  },
  created() {
    this.fetchData();
    this.timer = setInterval(this.fetchData, 5000);
  },
  methods: {
    async fetchData() {
      const res = await fetch("https://yesno.wtf/api");
      const data = await res.json();
      this.answer = data;
    },
    cancelAutoUpdate() {
      clearInterval(this.timer);
    },
  },
  beforeDestroy() {
    this.cancelAutoUpdate();
  },
};
</script>